package edu.ccut.saturn.basecomponent.baseservicecomponent;

import edu.ccut.saturn.component.ComponentFactory;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.ILogicContext;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.component.SaturnDataManager;
import edu.ccut.saturn.component.SaturnTaskManager;

public class Timer extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		String startTime = (String) parameterContext.getParameterValue(1);
		String endTime = (String) parameterContext.getParameterValue(2);
		String repeatCount = (String) parameterContext.getParameterValue(3);
		String repeatInterval = (String) parameterContext.getParameterValue(4);
		String repeatUnit = (String) parameterContext.getParameterValue(5);
		String cronExpression = (String) parameterContext.getParameterValue(6);
		String componentFullKey = (String) parameterContext
				.getParameterValue(7);

		SaturnData taskSaturnData = SaturnDataManager.getInstance().createData(
				"http://edu.ccut.saturn.basecomponent/data", "TASK");

		taskSaturnData.setString("startTime", startTime);
		taskSaturnData.setString("endTime", endTime);
		taskSaturnData.setString("repeatCount", repeatCount);
		taskSaturnData.setString("repeatInterval", repeatInterval);
		taskSaturnData.setString("repeatUnit", repeatUnit);
		taskSaturnData.setString("cronExpression", cronExpression);
		taskSaturnData.setString("componentFullKey", componentFullKey);

		IParameterContext logicParameterContext = getTaskParameterContext(parameterContext);

		try {
			String taskFullKey = SaturnTaskManager.getInstance().create(
					taskSaturnData, logicParameterContext);
			SaturnTaskManager.getInstance().start(taskFullKey);
			parameterContext.setParameter(0, taskFullKey);
		} catch (Exception e) {
			logger
					.error(
							"create task failed!! please check the startTime and endTime make sure the time is validate !",
							e);
		}
	}

	private IParameterContext getTaskParameterContext(IParameterContext parameterContext) {
		IParameterContext logicParameterContext = ComponentFactory.createParameterContext();

		if (parameterContext.getParameters().size() > 8) {
			for (int i = 8; i < parameterContext.getParameters().size(); i++) {
				if (parameterContext.getParameterValue(i) instanceof String) {
					String param = (String) parameterContext.getParameterValue(i);
					param = parseCondtion(param, parameterContext.getLogicContext());
					logicParameterContext.addParameter(param, String.class);
				} else {
					logicParameterContext.addParameter(parameterContext.getParameterValue(i), parameterContext.getParameter(i).getClassType());
				}
			}
		}

		return logicParameterContext;
	}

	protected String parseCondtion(String condition, ILogicContext context) {
		StringBuffer newCondition = new StringBuffer().append(""); //$NON-NLS-1$
		char[] charArray = condition.toCharArray();
		StringBuffer oldexp = new StringBuffer();// ${}表达式
		boolean isGetExp = false; // 是否在拼${}表达式
		boolean is$ = false;
		for (char currItem : charArray) {
			if (is$) {
				if (currItem == '{') {
					// 开始拼表达式
					isGetExp = true;
					oldexp.append("${"); //$NON-NLS-1$
				} else {
					// 只是普通$符号
					newCondition.append("$").append(currItem); //$NON-NLS-1$
				}
				is$ = false;
			} else {
				if (isGetExp) {
					if (currItem != '}') {
						oldexp.append(currItem);
					} else {
						oldexp.append(currItem);
						isGetExp = false;
						// 解析
					//	debug("oldexp=====" + oldexp.toString(), );
						Object value = context.getValue(oldexp.toString());
						if (value != null
								&& !"".equals(((String) value).trim())) {
							newCondition.append(value);
						} else {
							return ""; //$NON-NLS-1$
						}
						oldexp.delete(0, oldexp.length());
					}
				} else {
					if (currItem != '$') {
						newCondition.append(currItem);
					} else {
						// 判断是否开始拼表达式
						is$ = true;
					}
				}
			}
		}
		return newCondition.toString();
	}
}